package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.admin.mapper.SetmealMapper;
import com.itheima.admin.pojo.Setmeal;
import com.itheima.common.R;
import com.itheima.admin.mapper.CategoryMapper;
import com.itheima.admin.mapper.DishMapper;
import com.itheima.admin.pojo.Category;
import com.itheima.admin.pojo.Dish;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Author longbosheng
 * @Date 2022/6/13 19:37
 * @PackageName:com.itheima.service
 * @ClassName: CategoryServiceImpl
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /*
     * 使用mybatis-plus来作为数据库操作
     * 直接在service层调用方法完成
     * QueryWrapper<Category> wrapper = new QueryWrapper<>();
     * */
    @Override
    public R insertCategory(Category category) {
        int flag = categoryMapper.insert(category);
        if (flag > 0) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    /**
     * 分类查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public R selectCategory(int page, int pageSize) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");
         IPage<Category> pageC = new Page<>(page, pageSize);
        IPage<Category> categoryIPage = categoryMapper.selectPage(pageC, wrapper);
        System.out.println("查询的数据："+categoryIPage.getRecords());
        if (categoryIPage.getRecords().size() >= 0) {
            return R.success(categoryIPage);
        } else {
            return R.error("查询失败！");
        }
    }

    /**
     * 分类更新
     * @param category
     * @return
     */
    @Override
    public R updateCategory(Category category) {
        UpdateWrapper<Category> wrapper = new UpdateWrapper<>();
        String id = category.getId();
        wrapper.eq("id", id);
        int flag = categoryMapper.update(category, wrapper);
        if (flag>0){
            return R.success("修改分类信息成功");
        }
        return R.error("修改分类信息失败");
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @Override
    public R deleteCategory(String id) {
        /*检查套餐是否有该分类*/
        QueryWrapper<Setmeal> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("category_id", id);
        System.out.println("删除的id:"+id);
        List<Setmeal> setmeals = setmealMapper.selectList(wrapper2);
        if (setmeals.size()>0){
            return R.error("当前分类下关联了套餐，不能删除");
        }
        /*检查菜品下是否有改分类*/
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", id);
        System.out.println("删除的id:"+id);
        List<Dish> dishes = dishMapper.selectList(wrapper);
        if (dishes.size()>0){
            return R.error("删除失败,该分类别包含未删除的菜品");
        }

        int flag = categoryMapper.deleteById(id);
        if (flag>0){
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    /**
     * 下拉列表
     * @param type
     * @return
     */
    @Override
    public R DropDownList(int type) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type);
        List<Category> categories = categoryMapper.selectList(wrapper);
        if (categories.size()>=0){
            return R.success(categories);
        }
        return R.error("列表功能异常！");
    }
}
